package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService{

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void insert(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Department get(Long id) {
       return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    public PageInfo<Department> query(QueryObject qo) {

        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Department> departments = departmentMapper.queryForList(qo);
        return new PageInfo<>(departments);
        /*int totalCount = departmentMapper.queryForCount(qo);
        if (totalCount ==0){
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),
                    Collections.emptyList(),totalCount);
        }
        //获取总页数
        int totalPage = totalCount % qo.getPageSize() ==0 ? totalCount / qo.getPageSize()
                : totalCount / qo.getPageSize() +1;
        //当前页大于总页数
        if (qo.getCurrentPage() > totalPage){
            qo.setCurrentPage(totalPage);
        }
        //totalPage 不为0
        List<Department> departmentList = departmentMapper.queryForList(qo);
        return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),
                departmentList,totalCount);*/
    }
}
